<?php
require_once 'mods/class_generic_object.php';
require_once 'mods/class_collection.php';
require_once 'mods/human.php';
require_once 'mods/class_group.php';
class User extends GenericObject
{
    public $humans;
    public $groups;
    public function __construct()
    {
        parent::__construct();
        $this->_tableName='users';
        $this->_properties['id']=null;
        $this->_properties['login']=null;
        $this->_properties['password']=null;
        $this->_properties['group']=null;
        $this->_properties['human']=null;
        $this->_properties['description']=null;
        $this->humans=new Collection();
        $this->humans->setLoadCallback('loadHuman', $this);
        $this->groups=new Collection();
        $this->groups->setLoadCallback('loadGroup', $this);
    }
    
    public function loadHuman(Collection $col)
    {
        $human=new Human();
        $human->load($this->human);
        $col->addItem($human);
    }
    
    public function loadGroup(Collection $col)
    {
        $group=new Group();
        $group->load($this->group);
        $col->addItem($group);
    }
    
    public function getHuman()
    {
        return $this->humans->getItem();
    }
    
    public function getGroup()
    {
        return $this->groups->getItem();
    }
    
    public function set($id,$login,$group,$human,$description)
    {
        $this->id=$id;
        $this->login=$login;
        $this->group=$group;
        $this->human=$human;
        $this->description=$description;
    }
    
    public function load($id = null)
    {
        if (!empty($id)) {
            $id=intval($id);
            $sql='SELECT a.id,a.login,a.group,a.human,a.description,
            b.id,b.name,b.town,b.company,b.department,b.email,b.active 
            FROM users AS a, humans AS b 
            WHERE a.human=b.id AND a.id='.$id;
            $result=$this->_db->select($sql,false);
            $sizeofresult=sizeof($result,false);
            if ($sizeofresult) {
                list($id,$login,$group,$human,$description,
                     $human_id,$human_name,$human_town,$human_company,$human_department,
                     $human_email,$human_active)=$result[0];
                $this->set($id,$login,$group,$human,$description);
                /*$this->human=new Human();
                $this->human->set($human_id,$human_name,$human_town,$human_company,$human_department,
                     $human_email,$human_active,null);*/
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
?>